//
//  ViewController.m
//  WangI
//
//  Created by wjn on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "ViewController.h"
#import "SecondModel.h"
#import "ScrollWithImage.h"
#import "tableCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

//@property(nonatomic, retain)FirstModel *firstmodel;
@property(nonatomic, retain)NSMutableArray *dataSource;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UIView *viewtiao;
@property(nonatomic, retain)NSMutableArray *dataSourceT;
@property(nonatomic, retain)NSMutableArray *dataSourceY;
@property(nonatomic, retain)NSMutableArray *dataSourceC;

@property(nonatomic, retain)UIButton *toutiaobutton;
@property(nonatomic, retain)UIButton *yulebutton;
@property(nonatomic, retain)UIButton *tiyubutton;
@property(nonatomic, retain)UIButton *caijingbutton;


@end

@implementation ViewController


- (void)dealloc {
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent=NO;
    self.navigationItem.title = @"网易新闻";

    [self getURL];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.collectionView];

    
    _viewtiao=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    _viewtiao.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_viewtiao];
    
    
    
    _toutiaobutton=[UIButton buttonWithType:UIButtonTypeSystem];
    _yulebutton=[UIButton buttonWithType:UIButtonTypeSystem];
    _tiyubutton=[UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *redianbutton=[UIButton buttonWithType:UIButtonTypeSystem];
    _caijingbutton=[UIButton buttonWithType:UIButtonTypeSystem];
    [_toutiaobutton setTitle:@"头条" forState:UIControlStateNormal];
    [_yulebutton setTitle:@"娱乐" forState:UIControlStateNormal];
    [_tiyubutton setTitle:@"体育" forState:UIControlStateNormal];
    [redianbutton setTitle:@"热点" forState:UIControlStateNormal];
    [_caijingbutton setTitle:@"财经" forState:UIControlStateNormal];
    
    
    
    
    
    _toutiaobutton.frame=CGRectMake(0, 0, self.view.bounds.size.width/5, 40);
    _yulebutton.frame=CGRectMake(self.view.bounds.size.width/5, 0, self.view.bounds.size.width/5, 40);
    _tiyubutton.frame=CGRectMake(self.view.bounds.size.width/5*2, 0, self.view.bounds.size.width/5, 40);
    _caijingbutton.frame=CGRectMake(self.view.bounds.size.width/5*3, 0, self.view.bounds.size.width/5, 40);
    redianbutton.frame=CGRectMake(self.view.bounds.size.width/5*4, 0, self.view.bounds.size.width/5, 40);
    
    
    
    
    
    [_viewtiao addSubview:_toutiaobutton];
    [_viewtiao addSubview:_tiyubutton];
    [_viewtiao addSubview:_yulebutton];
    [_viewtiao addSubview:_caijingbutton];
    [_viewtiao addSubview:redianbutton];
    
  
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        layout.itemSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
        layout.minimumInteritemSpacing=0;
        layout.minimumLineSpacing=0;
        _collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.pagingEnabled=YES;
        
        [_collectionView registerClass:[tableCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewIdentifier"];
        
        
        
    }
    return _collectionView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    tableCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewIdentifier" forIndexPath:indexPath];
    if (indexPath.row == 0) {
       
            cell.arrayaaa=_dataSource;
      
    }
    
    if (indexPath.row == 1) {
        
            cell.arrayaaa=_dataSourceY;
       
    }
    if (indexPath.item==2) {
        
            cell.arrayaaa=_dataSourceT;
        
    }
    if (indexPath.item==3) {
        
            cell.arrayaaa=_dataSourceC;
        
    }
    
    return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"偏移了%f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x == 414.00*0) {
        [_yulebutton setBackgroundColor:[UIColor clearColor]];
        [_tiyubutton setBackgroundColor:[UIColor clearColor]];
        [_caijingbutton setBackgroundColor:[UIColor clearColor]];
        [_toutiaobutton setBackgroundColor:[UIColor redColor]];
    }
    if (scrollView.contentOffset.x == 414.00) {
        [_yulebutton setBackgroundColor:[UIColor redColor]];
        [_tiyubutton setBackgroundColor:[UIColor clearColor]];
        [_caijingbutton setBackgroundColor:[UIColor clearColor]];
        [_toutiaobutton setBackgroundColor:[UIColor clearColor]];
    }
    if (scrollView.contentOffset.x == 414.00*2) {
        [_yulebutton setBackgroundColor:[UIColor clearColor]];
        [_tiyubutton setBackgroundColor:[UIColor redColor]];
        [_caijingbutton setBackgroundColor:[UIColor clearColor]];
        [_toutiaobutton setBackgroundColor:[UIColor clearColor]];
    }
    if (scrollView.contentOffset.x == 414.00*3) {
        [_yulebutton setBackgroundColor:[UIColor clearColor]];
        [_tiyubutton setBackgroundColor:[UIColor clearColor]];
        [_caijingbutton setBackgroundColor:[UIColor redColor]];
        [_toutiaobutton setBackgroundColor:[UIColor clearColor]];
    }
    
}
-(void)getURL{
    [SANetWorkingTask request:kNewsGETURL parmater:nil block:^(id result) {
        
        NSArray *array=[result valueForKey:@"T1348647853363"];
        
        for (NSDictionary *dic in array) {
            SecondModel *second=[[SecondModel alloc]initWithDictionary:dic];
            [self.dataSource addObject:second];
        }
        [_collectionView reloadData];
   
    }];
    [SANetWorkingTask request:kCaijingGETURL parmater:nil block:^(id result) {
        
        NSArray *array=[result valueForKey:@"T1348648756099"];
        
        for (NSDictionary *dic in array) {
            SecondModel *second=[[SecondModel alloc]initWithDictionary:dic];
            [self.dataSourceC addObject:second];
        }
        [_collectionView reloadData];
        
    }];
    [SANetWorkingTask request:kTiyuGETURL parmater:nil block:^(id result) {
        
        NSArray *array=[result valueForKey:@"T1348649079062"];
    
        for (NSDictionary *dic in array) {
            SecondModel *second=[[SecondModel alloc]initWithDictionary:dic];
            [self.dataSourceT addObject:second];
        }
        [_collectionView reloadData];
    
    }];
    [SANetWorkingTask request:kYuleGETURL parmater:nil block:^(id result) {

        NSArray *array=[result valueForKey:@"T1348648517839"];

        for (NSDictionary *dic in array) {
            SecondModel *second=[[SecondModel alloc]initWithDictionary:dic];
            [self.dataSourceY addObject:second];
        }
        [_collectionView reloadData];

    }];




    
}



-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}
-(NSMutableArray *)dataSourceT{
    if (!_dataSourceT) {
        self.dataSourceT=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceT;
}
-(NSMutableArray *)dataSourceY{
    if (!_dataSourceY) {
        self.dataSourceY=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceY;
}
-(NSMutableArray *)dataSourceC{
    if (!_dataSourceC) {
        self.dataSourceC=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
